package org.walker.iotScheduling.modules.iot.v2.job;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;
import org.walker.iotScheduling.modules.iot.v2.factory.TimeStrategyFactory;
import org.walker.iotScheduling.modules.iot.v2.strategy.TimeStrategy;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

/**
 * @author :zhuangguojin
 * @since :2024/12/4 17:10
 */
@Component("schedulerJob_V2")
@RequiredArgsConstructor(onConstructor_ = {@Lazy, @Autowired})
public class SchedulerJob {
    @Resource(name = "ThreadPoolSchedulerCommon")
    private final ThreadPoolTaskScheduler scheduler;
    private final TimeStrategyFactory strategyFactory;

    @PostConstruct
    public void manageSchedulerJob() {
        // 注册任务
        strategyFactory.registerStrategy("specialTask", "0/5 * * * * ?");
        strategyFactory.registerStrategy("commonTask", "0/10 * * * * ?");
        strategyFactory.registerStrategy("newTask", "0/20 * * * * ?");

        // 获取任务
        List<TimeStrategy> strategies = strategyFactory.getAllStrategies();
        // 注入scheduler执行TimeStrategy.scheduleTask
        strategies.forEach(o -> {
            scheduler.schedule(o::scheduleTask,o.getCronTrigger());
        });
    }
}
